For the sake of this article, we will build an almost working driver for the Redis key-value store. A Redis server is just a TCP server sends and receives messages. Redis uses its own protocol (more on this in a while) on top of TCP to exchange data, without relying on common protocols such as HTTP, but we will not focus on that: we will only deal with the TCP connection from Elixir to the Redis server.
Have you ever wanted or had the need to take a database table and cram a lot of information into a specific field using JSON?
This is possible and supported by both PostgreSQL and Ecto, and it can sometimes simplify your database in cases where you absolutely want to store some kind of nested data structure.
This post shortly describes how to create embedded schemas in Ecto to save a JSON data structure on your database, and how to implement a direct database search on this same field.
PostgreSQL has great support for objects stored as JSON. This is useful for those moments when you need to store data that could be variably structured, such as responses from other services’ APIs, or data that frequently travels together within your relational tables.
A common trade-off for mixing scalar column data types (like varchar or integer) with column data types that handle more-complicated objects (like JSON) is that ORMs or data mappers sometimes can’t introspect on them for you, which means it becomes much harder to query that data.
Using Ecto’s embedded_schema helps introspect on those known values, but it doesn’t really assist you with querying those fields in SQL. This is where I became extremely greatful for Ecto’s escape hatch: fragment().
As developers, many of us build breakable toys in various programming languages when learning something new; the goal is to learn the language and not the intricacies of a new product, after all! Some of us reach for todo apps, others a messaging clone. If you’re like me, though, it’s URL shorteners.
I built my most recent URL shortener in Elixir, and decided to see how much raw speed I could get out of the application by using GenServer - Generic Server - to build out a key-value store to bypass the database when possible.
If you have been around the Elixir ecosystem for a while, you have probably heard about “ETS”.
ETS stands for Erlang Term Storage. It’s a storage engine that is built into OTP, and is therefore available in Elixir through the interoperability with Erlang.
ETS is a key / value store, and so you can think of it as pretty much the same as Redis. But because ETS is built right into OTP, you get it for free, without having to rely on a third-party dependancy!
In today’s tutorial we are going be looking at using ETS in Elixir.
The next step of our exploration is to investigate GenServer. Whilst Agents are specifically used for storing state, GenServer is used as a general purpose abstraction for building generic servers in Elixir.
In today’s tutorial we will be exploring the how and why of using GenServer.